/* 公共函数 */
function voteToStarsArray(vote) {
  /**
   * 输入电影的评分，输出一个array，用于显示星星
   * 满分10分=5星
   */
  let star = Math.ceil(vote / 2); //整除向上取整
  let stars = new Array();
  for (var i = 1; i <= 5; i++) {
    if (i <= star) {
      stars.push(1);
    } else {
      stars.push(0);
    }
  }
  return stars;
}

/**
 * Date类型转 yyyy-mm-dd 字符串
 */
function f_d2s_yyyymmdd(date) {
  var y = date.getFullYear(); //年
  var m = date.getMonth() + 1; //月
  m = m < 10 ? ('0' + m) : m;
  var d = date.getDate(); //日
  d = d < 10 ? ('0' + d) : d;

  // var h = date.getHours();
  // var minute = date.getMinutes();
  // minute = minute < 10 ? ('0' + minute) : minute;
  // var second = date.getSeconds();
  // second = minute < 10 ? ('0' + second) : second;
  // return y + '-' + m + '-' + d + ' ' + h + ':' + minute + ':' + second;
  return y + '-' + m + '-' + d;
}


/**
 * 根据id取url
 */
function Movies_GetListUrlFromListId(movieListId, pageNo) {
  let page = pageNo + 1
  console.log("page=" + page)
  let app = getApp()
  let d = new Date();
  let movieListUrl = "";
  // 页面间参数传递是字符串类型，这里需要转成int
  movieListId = parseInt(movieListId)
  switch (movieListId) {
    case 0:
      //正在热映
      movieListUrl = app.globalData.tmdb_query_url[movieListId].queryUrl + "&api_key=" + app.globalData.tmdb_api_key_v3 + "&primary_release_date.lte=" + f_d2s_yyyymmdd(d) + "&page=" + page;
      break
    case 1:
      //即将上映，发行日期大于今日
      movieListUrl = app.globalData.tmdb_query_url[movieListId].queryUrl + "&api_key=" + app.globalData.tmdb_api_key_v3 + "&primary_release_date.gte=" + f_d2s_yyyymmdd(d) + "&page=" + page;
      break
    case 2:
      //top
      movieListUrl = app.globalData.tmdb_query_url[movieListId].queryUrl + "&api_key=" + app.globalData.tmdb_api_key_v3 + "&page=" + page;
      break
  };
  console.log("movieListUrl: " + movieListUrl)
  return movieListUrl;
}

/**
 * 输入 movie list的url，执行查询; 结果给回调函数
 * url 电影list的url 可以从 Movies_GetListUrlFromListId 获取
 * idx 电影list 的id
 * movie_lens 需要返回的记录长度
 */
function Movies_RequestMovieList(url, idx, movie_lens, callback) {
  console.log(url)
  wx.request({
    url: url,
    method: "GET",
    header: {
      "Content-Type": "application/json;charset=utf-8"
    },
    success: function(res) {
      console.log(res.data.results)
      callback(res.data.results, idx, movie_lens)
    },
    fail: function(error) {
      console.error("api 执行失败" + error)
    },
    complete: function() {
      console.log("api 执行完毕")
    }
  });
}

/**
 * 和上一个区别在，返回的是data；
 */
function tmdb_movie(url, callback) {
  console.log(url)
  wx.request({
    url: url,
    method: "GET",
    header: {
      "Content-Type": "application/json;charset=utf-8"
    },
    success: function(res) {
      console.log(res.data)
      callback(res.data)
    },
    fail: function(error) {
      console.error("api 执行失败" + error)
    },
    complete: function() {
      console.log("api 执行完毕")
    }
  });
}


module.exports.voteToStarsArray = voteToStarsArray
module.exports.Movies_GetListUrlFromListId = Movies_GetListUrlFromListId
module.exports.Movies_RequestMovieList = Movies_RequestMovieList
module.exports.tmdb_movie = tmdb_movie